#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# Copyright © 2018-2021 Arm Limited. All rights reserved.
# SPDX-License-Identifier: Apache-2.0
#

import os
import common

Import('env')

common.setup_common_env(env)
common.setup_toolchain(env, env['platform'])

# Note we *prepend* so these take priority over CPATH command-line-arguments to avoid depending on the install target
# where the install target is also provided via CPATH.
env.PrependUnique(CPPPATH=[os.path.join(env['command_stream_dir'], 'include'),
                           os.path.join(env['utils_dir'], 'include'),
                           'src'])

# Build support_library shared and static libs
srcs = [os.path.join('src', 'Support.cpp'),
        os.path.join('src', 'CapabilitiesInternal.cpp'),
        os.path.join('src', 'SupportQueries.cpp'),
        os.path.join('src', 'Network.cpp'),
        os.path.join('src', 'Operation.cpp'),
        os.path.join('src', 'ConcreteOperations.cpp'),
        os.path.join('src', 'Compiler.cpp'),
        os.path.join('src', 'nonCascading', 'BufferManager.cpp'),
        os.path.join('src', 'WeightEncoder.cpp'),
        os.path.join('src', 'nonCascading', 'Strategies.cpp'),
        os.path.join('src', 'nonCascading', 'StrategyX.cpp'),
        os.path.join('src', 'Graph.cpp'),
        os.path.join('src', 'GraphNodes.cpp'),
        os.path.join('src', 'NetworkToGraphConverter.cpp'),
        os.path.join('src', 'nonCascading', 'Pass.cpp'),
        os.path.join('src', 'nonCascading', 'McePlePass.cpp'),
        os.path.join('src', 'nonCascading', 'PlePass.cpp'),
        os.path.join('src', 'nonCascading', 'ConversionPass.cpp'),
        os.path.join('src', 'nonCascading', 'SpaceToDepthPass.cpp'),
        os.path.join('src', 'nonCascading', 'Section.cpp'),
        os.path.join('src', 'SubmapFilter.cpp'),
        os.path.join('src', 'SramAllocator.cpp'),
        os.path.join('src', 'Utils.cpp'),
        os.path.join('src', 'DebuggingContext.cpp'),
        os.path.join('src', 'Optimization.cpp'),
        os.path.join('src', 'PerformanceData.cpp'),
        os.path.join('src', 'cascading', 'Cascading.cpp'),
        os.path.join('src', 'cascading', 'Part.cpp'),
        os.path.join('src', 'cascading', 'Plan.cpp'),
        os.path.join('src', 'cascading', 'Combiner.cpp'),
        os.path.join('src', 'cascading', 'Visualisation.cpp'),
        os.path.join('src', 'nonCascading', 'NonCascading.cpp'),
        os.path.join('src', 'cascading', 'Estimation.cpp'),
        os.path.join('src', 'cascading', 'EstimationUtils.cpp'),
        os.path.join('src', 'cascading', 'MceEstimationUtils.cpp')]

ethosn_support_lib = env.StaticLibrary('libEthosNSupport', srcs)
env.Alias('install', env.Install(env['install_lib_dir'], ethosn_support_lib))

# Note we must explicitly set the SONAME to override the default which uses a path relative to the current directory.
# This means that when the runtime linker looks for the .so, it uses the filename only and not the relative path.
# We need to clone the env to make sure we don't pollute these LINKFLAGS into subsequent builders.
# Note: can't just pass this special LINKFLAGS to SharedLibrary as that would override the existing one we have,
#       which may have --coverage and other flags.
soEnv = env.Clone()
soEnv.AppendUnique(LINKFLAGS='-Wl,-soname=libEthosNSupport.so')
# On non-Unix platforms, the static and shared lib filenames may collide, so use a different name
shared_lib_name = 'libEthosNSupport' if env['PLATFORM'] == 'posix' else 'libEthosNSupportShared'
# On non-Unix platforms, the static and shared libs may need to have different compile options, so the obj names
# need to be unique
obj_prefix = None if env['PLATFORM'] == 'posix' else 'Shared_'
ethosn_support_shared = soEnv.SharedLibrary(shared_lib_name, srcs, SHOBJPREFIX=obj_prefix, LIBS=['pthread'])

env.Alias('install', env.Install(env['install_lib_dir'], ethosn_support_shared))
env.Alias('install', env.Install(os.path.join(env['install_include_dir'], 'ethosn_support_library'),
                                 Glob(os.path.join('include', 'ethosn_support_library', '*'))))

# Build unit tests, if requested.
if env['tests']:
    SConscript(dirs='tests', duplicate=False, exports=['env', 'ethosn_support_shared'])
